<?php

namespace app\xwam\validate;

/**
 * 管理员验证类
 *
 * @version 2018-1-26 sochishun added.
 */
class AdminValidate extends \think\Validate {

    // 批量验证
    protected $batch = true;
    // 当前验证的规则
    protected $rule = [
        'user_name' => 'require|max:25',
        'user_name' => 'checkExists',
        'password' => 'require|max:25',
        'email' => 'email',
        'status' => 'number',
    ];
    // 验证字段描述
    protected $field = [
        'user_name' => '用户名',
        'password' => '密码',
        'email' => '邮箱',
        'status' => '状态',
    ];
    // 验证提示信息
    protected $message = [
        'user_name.checkExists' => '用户名已存在!',
    ];
    // 验证场景
    protected $scene = [
        'add' => ['user_name', 'password'],
        'edit' => ['email'],
    ];

    // 检测用户名是否重复 2016-4-8 SoChishun Added.
    function checkExists($val) {
        $n = db('user_admin')->where(array('user_name' => trim($val)))->count('id');
        return $n < 1;
    }

}
